package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportSmsTemplate;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportSmsTemplateDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:14:33
 */
public class TransportSmsTemplateConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportSmsTemplateConverts.class);

    public static List<TransportSmsTemplateDTO> convert(List<TransportSmsTemplate> transportSmsTemplateList) {
        if (CollectionUtils.isEmpty(transportSmsTemplateList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportSmsTemplateDTO> transportSmsTemplateListResult = Lists.newArrayList();
        for (TransportSmsTemplate transportSmsTemplate : transportSmsTemplateList) {
            transportSmsTemplateListResult.add(convert(transportSmsTemplate));
        }
        return transportSmsTemplateListResult;
    }

    public static TransportSmsTemplateDTO convert(TransportSmsTemplate transportSmsTemplate) {
        try {
            return BeanUtils.copy(transportSmsTemplate, TransportSmsTemplateDTO.class);
        } catch (Exception e) {
            LOG.error("TransportSmsTemplate到TransportSmsTemplateDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportSmsTemplate到TransportSmsTemplateDTO转换失败", e);
        }
    }

    public static TransportSmsTemplate convert(TransportSmsTemplateDTO transportSmsTemplateDTO) {
        try {
            return BeanUtils.copy(transportSmsTemplateDTO, TransportSmsTemplate.class);
        } catch (Exception e) {
            LOG.error("TransportSmsTemplateDTO到TransportSmsTemplate转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportSmsTemplateDTO到TransportSmsTemplate转换失败", e);
        }
    }
}